<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授课老师" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入授课老师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jiaopei:sortCurriculum:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['jiaopei:sortCurriculum:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jiaopei:sortCurriculum:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['jiaopei:sortCurriculum:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sortCurriculumList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" v-if="true" min-width="60"  show-overflow-tooltip />
      <el-table-column label="班级" align="center" prop="classVo.name" min-width="100"  show-overflow-tooltip />
      <el-table-column label="授课老师" align="center" prop="teacherVo.nickName" min-width="80" />

      <el-table-column label="上课日期" align="center" prop="startDate" min-width="96"   />
      <el-table-column label="结束日期" align="center" prop="endDate" min-width="96" />
      <el-table-column label="开始时间" align="center" prop="startHour" min-width="80" />
      <el-table-column label="结束时间" align="center" prop="endHour" min-width="80" />
      <el-table-column label="重复次数" align="center" prop="frequency" min-width="80" />

      <el-table-column label="每节课时" align="center" prop="lessonHours" min-width="80" />
      <el-table-column label="类型" align="center" prop="type" min-width="60" :formatter="typeName" />
      <el-table-column label="每周几" align="center" prop="weekly" />

      <el-table-column label="课节数量" align="center" prop="sl" min-width="80" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="showKj(scope.row)"
            v-hasPermi="['jiaopei:kj:list']"
          >{{scope.row.sl}}</el-button>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createdDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right"  align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['jiaopei:sortCurriculum:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['jiaopei:sortCurriculum:remove']"-->
<!--          >删除</el-button>-->

          <el-button
            size="mini"
            type="text"
            @click="showKj(scope.row)"
            v-hasPermi="['jiaopei:kj:list']"
          >课节</el-button>


        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 排课 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="班级" prop="className">
          <el-input v-model="form.className" readonly placeholder="请选择班级" >
            <i  class="el-icon-edit el-input__icon" slot="suffix" @click="openSelectClass" />
          </el-input>
        </el-form-item>

        <el-form-item label="授课老师" prop="teacherName">
          <el-input v-model="form.teacherName" readonly placeholder="请输入授课老师" >
            <i  class="el-icon-edit el-input__icon" slot="suffix" @click="openSelectUser" />
          </el-input>
        </el-form-item>

        <el-form-item label="每节课时" prop="lessonHours">
          <el-input-number v-model="form.lessonHours" :min="1" :max="99999"  placeholder="请输入每节课时"></el-input-number>
        </el-form-item>


        <el-form-item label="上课日期" prop="startDate">
          <el-date-picker clearable
                          v-model="form.startDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择上课日期">
          </el-date-picker>

        </el-form-item>


        <el-form-item label="开始时间" prop="startHour">
          <el-time-select
            style="width:180px"
            size="small"
            placeholder="起始时间"
            v-model="form.startHour"
            :picker-options="{start: '06:00',step: '00:10',end: '23:00'}">
          </el-time-select>
          <el-time-select
            style="width:180px;margin-left:16px;"
            size="small"
            placeholder="结束时间"
            v-model="form.endHour"
            :picker-options="{start: '06:30',step: '00:10',end: '23:00',minTime:form.startHour}">
          </el-time-select>
        </el-form-item>


        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type" >
            <el-radio-button label="1">不重复</el-radio-button>
            <el-radio-button label="2">每天</el-radio-button>
            <el-radio-button label="3">每周</el-radio-button>
          </el-radio-group>
        </el-form-item>


        <el-form-item label="每周几" prop="weeklys" v-show="form.type=='3' " >
          <el-checkbox-group v-model="form.weeklys" size="mini">
            <el-checkbox size="mini" label="2">周一</el-checkbox>
            <el-checkbox size="mini" label="3">周二</el-checkbox>
            <el-checkbox size="mini" label="4">周三</el-checkbox>
            <el-checkbox size="mini" label="5">周四</el-checkbox>
            <el-checkbox size="mini" label="6">周五</el-checkbox>
            <el-checkbox size="mini" label="7">周六</el-checkbox>
            <el-checkbox size="mini" label="1">周日</el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item label="结束方式"  prop="frequency" v-show="form.type!='1' " >

          <el-radio v-model="form.jsfs" label="1">
            重复一定次数结束
            <el-input-number v-model="form.frequency" size="mini" placeholder="请输入重复次数"></el-input-number>
          </el-radio>

          <el-radio v-model="form.jsfs" label="2" style="margin-top:20px;" >
            到期结束
            <el-date-picker clearable
                            v-model="form.endDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择结束日期">
            </el-date-picker>
          </el-radio>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <select-class ref="selectClass"  @ok="selectClassOk" />

    <select-user ref="select"  @ok="selectUserOk" />


  </div>
</template>

<script>
import { listSortCurriculum, getSortCurriculum, delSortCurriculum, addSortCurriculum, updateSortCurriculum } from "@/api/jiaopei/sortCurriculum";
import selectClass from "@/views/jiaopei/class/selectClass";
import selectUser from "@/views/jiaopei/class/selectUser";


export default {
  name: "SortCurriculum",
  components: { selectClass,selectUser },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排课表格数据
      sortCurriculumList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classId: undefined,
        className: undefined,
        startDate: undefined,
        endDate: undefined,
        startHour: undefined,
        endHour: undefined,
        type: undefined,
        frequency: undefined,
        weekly:undefined,
        teacherId: undefined,
        teacherName: undefined,
        lessonHours: undefined,
        createdDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "ID不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "班级ID不能为空", trigger: "blur" }
        ],
        startDate: [
          { required: true, message: "上课日期不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "结束日期不能为空", trigger: "blur" }
        ],
        startHour: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endHour: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        frequency: [
          { required: true, message: "重复次数不能为空", trigger: "blur" }
        ],
        weekly: [
          { required: true, message: "每周几不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "授课老师不能为空", trigger: "blur" }
        ],
        lessonHours: [
          { required: true, message: "每节课时不能为空", trigger: "blur" }
        ],
        createdDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      },
      leftUrl:undefined
    };
  },
  watch: {
    $route: {
      immediate: true,
      handler(to, from) {
        if(to.path=='/yewu/sortCurriculum'){
          if(this.leftUrl!=to.fullPath){

            if(this.$route.query['className']){
              this.queryParams.className=this.$route.query['className'];
            }

            this.getList();
          }
          this.leftUrl=to.fullPath;
        }
      }
    }
  },
  created() {
    if(this.$route.query['className']){
      this.queryParams.className=this.$route.query['className'];
    }

    this.getList();
  },
  methods: {
    /** 查询排课列表 */
    getList() {
      this.loading = true;
      listSortCurriculum(this.queryParams).then(response => {
        this.sortCurriculumList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        classId: undefined,
        startDate: undefined,
        endDate: undefined,
        startHour: undefined,
        endHour: undefined,
        type: '1',
        jsfs: '1',
        frequency: 1,
        weeklys:[],
        weekly:undefined,
        teacherId: undefined,
        teacherName: undefined,
        lessonHours: 1,
        createdDate: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排课";

      if(this.$route.query['teacherId']){
        this.form.teacherId=this.$route.query['teacherId'];
      }
      if(this.$route.query['teacherName']){
        this.form.teacherName=this.$route.query['teacherName'];
      }

      if(this.$route.query['classId']){
        this.form.classId=this.$route.query['classId'];
      }
      if(this.$route.query['className']){
        this.form.className=this.$route.query['className'];
      }


    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSortCurriculum(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改排课";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateSortCurriculum(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {

            if(this.form.type=='1'){
              this.form.frequency=1;
            }else if(this.form.type=='2'){

            }else if(this.form.type=='3'){
              if(this.form.weeklys.length>0){
                this.form.weekly = this.form.weeklys.join(',');
              }

            }

            addSortCurriculum(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除排课编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delSortCurriculum(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jiaopei/sortCurriculum/export', {
        ...this.queryParams
      }, `sortCurriculum_${new Date().getTime()}.xlsx`)
    },
    openSelectClass(){
      this.$refs.selectClass.show();
    },
    selectClassOk(row){
      this.form.classId=row['id'];
      this.form.className=row['name'];

      this.form.teacherId=row['teacherId'];
      this.form.teacherName=row['teacherVo']['nickName'];

      this.$forceUpdate();
    },
    openSelectUser() {
      this.$refs.select.show();
    },
    selectUserOk(row){
      this.form.teacherId=row['userId'];
      this.form.teacherName=row['nickName'];
      this.$forceUpdate();
    },
    typeName(row, column, cellValue, index){
      if(row['type']=='1'){
        return '不重复';
      }else if(row['type']=='2'){
        return '每天';
      }else if(row['type']=='3'){
        return '每周';
      }
    },
    showKj(row){
      const sortCurriculumId = row.id;
      this.$router.push({path:'/yewu/kj', query:{'sortCurriculumId':sortCurriculumId}})
    }
  }
};
</script>
